package com.example.aaa_viewpager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.example.aaa_viewpager.entity.User;
import com.example.aaa_viewpager.service.UserDAO;
import com.example.bb_serch.R;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter
{
	private LayoutInflater inflater;
	private ArrayList<HashMap> list;
	//private Context context;
	
	// private Context context;
	
	//// MyAdapter建構子
	public MyAdapter( Context context, ArrayList<HashMap> list)
	{
		// 還沒被載入或要動態載入的界面使用Inflater
		inflater = LayoutInflater.from(context);
		
		// 傳入的集合，並寫入MyAdapter class的arrayList的attribute
		this.list = list;
	}
	
	//計算集合內有多少筆資料
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	
	// 取得某一列的內容
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}
	
	// 取得某一列的id
	// listView點選位置
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	
	// 此方法就是將arrayList集合內的內容依順序方式一筆一筆放到ListView
	// 一筆就是一列View，全部列加起來就是一個listView
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		TagView tag;
		byte[] pic;
		UserDAO userDao = new UserDAO();
		
		// 此判斷式用來判斷View是否以載入過，View==null就重新載入,View!=null就不重新載入(調用記憶體的)
		if(convertView == null)
			// 使用inflate載入界面
		{
			convertView = inflater.inflate(R.layout.item_n, null);
			
			// 取得item.xml三個元件set到convertView
			tag = new TagView((ImageView) convertView.findViewById(R.id.img),
							  (TextView) convertView.findViewById(R.id.name),
							  (TextView) convertView.findViewById(R.id.address));
		
			convertView.setTag(tag);
		}
		else
		{
			// convertView不為null就直接中記憶體將Tag兩個元件取出
			tag = (TagView) convertView.getTag();
		}
		
		// 取得arrayList集合內的直並用setText方式寫入，執行後可看到結果
		tag.bb_name.setText(list.get(position).get("name").toString());
		tag.bb_address.setText(list.get(position).get("address").toString());
		
		// 取得arrayList集合內的值並進行轉型(資料庫取出為byte[]，需轉為Bitmap才可在畫面上呈現)
		pic = (byte[]) (list.get(position).get("picture"));
		Bitmap bmpout = BitmapFactory.decodeByteArray(pic, 0, pic.length);
		tag.bb_img.setImageBitmap(bmpout);
		return convertView;
	}
	
	//以下是讓MyAdapter的getView方法內if判斷式的TagView使用，set跟get元件用的
	class TagView
	{
		private ImageView bb_img;
		private TextView bb_name;
		private TextView bb_address;
		
		public TagView(ImageView bb_img,TextView bb_name,TextView bb_address)
		{
			this.bb_img = bb_img;
			this.bb_name = bb_name;
			this.bb_address = bb_address;
		}
		
	}
		
}
